package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Oilrecord;
import com.woniuxy.caradmin.entity.vo.OilRecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.OilrecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/oilrecord")
public class OilrecordController {
    @Autowired
    private OilrecordService oilrecordService;
    @GetMapping("/oilrecordList")
    @ApiOperation("查询所有的加油记录")
    public DataGridView selectAllOilRecords(OilRecordVo oilRecordVo) {
        Page<Object> page = PageHelper.startPage(oilRecordVo.getPage(), oilRecordVo.getLimit());
        List<Oilrecord> oilrecords = oilrecordService.selectAllOilRecords(oilRecordVo);
        if (oilrecords != null && oilrecords.size() > 0) {
            return new DataGridView(page.getTotal(), oilrecords);
        } else {
            throw new CarException(ResultCode.OILRECORDISNULL, Constaint.OILRECORDISNULL);
        }
    }

    //1 分页查询
    @GetMapping("/oilrecords")
    @ApiOperation("分页查询所有的加油记录")
    public DataGridView selectAllBy(OilRecordVo oilRecordVo) {
        Page<Object> page = PageHelper.startPage(oilRecordVo.getPage(), oilRecordVo.getLimit());
        List<Oilrecord> oilrecords = oilrecordService.selectAllBy(oilRecordVo);
        if (oilrecords != null && oilrecords.size() > 0) {
            return new DataGridView(page.getTotal(), oilrecords);
        } else {
            throw new CarException(ResultCode.OILRECORDISNULL, Constaint.OILRECORDISNULL);
        }

    }

    //2 增加加油记录
    @GetMapping("/addOilRecord")
    @ApiOperation("增加加油记录")
    public R addOilRecord(Oilrecord oilrecord, HttpServletRequest httpServletRequest) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //得到
        String vid = httpServletRequest.getParameter("vid");
        String oilstationid = httpServletRequest.getParameter("oilstationid");
        String time = httpServletRequest.getParameter("time");
        String oillableid = httpServletRequest.getParameter("oillableid");
        String priceone = httpServletRequest.getParameter("priceone");
        String totalprice = httpServletRequest.getParameter("totalprice");
        String nowmileage = httpServletRequest.getParameter("nowmileage");
        String lastmileage = httpServletRequest.getParameter("lastmileage");
        String information = httpServletRequest.getParameter("information");
        String driverid = httpServletRequest.getParameter("driverid");
        String status = httpServletRequest.getParameter("status");//设置增加的状态

        //时间处理
        java.sql.Date t1 = null;
        try {
            Date st1 = sdf.parse(time);
            t1 = new java.sql.Date(st1.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //设置
        oilrecord.setVid(Integer.parseInt(vid));
        oilrecord.setOilstationid(Integer.parseInt(oilstationid));
        //设置时间
        oilrecord.setTime(t1);
        oilrecord.setOillableid(Integer.parseInt(oillableid));
        oilrecord.setTotalprice(Double.valueOf(totalprice));
        oilrecord.setNowmileage(nowmileage);
        oilrecord.setLastmileage(lastmileage);
        oilrecord.setInformation(information);
        oilrecord.setDriverid(Integer.parseInt(driverid));
        oilrecord.setStatus(Integer.parseInt(status));

        int addOilRecord = oilrecordService.addOilRecord(oilrecord);
        if (addOilRecord != 0) {
            return R.ok().code(ResultCode.OILRECORDADDISSUC).message(Constaint.OILRECORDADDISSUC);
        } else {
            throw new CarException(ResultCode.OILRECORDADDISFAI, Constaint.OILRECORDADDISFAI);
        }
    }

    @GetMapping("/updateOilRecord")
    @ApiOperation("修改加油记录")
    public R updateOilRecord(Oilrecord oilrecord) {
        int updateOilRecord = oilrecordService.updateOilRecord(oilrecord);
        if (updateOilRecord != 0) {
            return R.ok().code(ResultCode.OILRECORDUPDATEISSUC).message(Constaint.OILRECORDUPDATEISSUC);
        } else {
            throw new CarException(ResultCode.OILRECORDUPDATEISFAI, Constaint.OILRECORDUPDATEISFAI);
        }
    }

    @GetMapping("/deleteOilRecord")
    @ApiOperation("删除加油记录")
    public R deleteOilRecord(Integer[] ids) {
        boolean deleteOilRecord = oilrecordService.deleteOilRecord(ids);
        if (deleteOilRecord) {
            return R.ok().code(ResultCode.OILRECORDDELETEISSUC).message(Constaint.OILRECORDDELETEISSUC);
        } else {
            throw new CarException(ResultCode.OILRECORDDELETEISFAI, Constaint.OILRECORDDELETEISFAI);
        }

    }

    @GetMapping("/oilRecordById")
    @ApiOperation("根据id查询加油记录")
    public R oilRecordById(Integer id){
        Oilrecord oilRecordById = oilrecordService.oilRecordById(id);
        if (oilRecordById !=null) {
            return R.ok().data("oilRecordById",oilRecordById);
        } else {
            throw new CarException(ResultCode.OILRECORDISNULL, Constaint.OILRECORDISNULL);
        }
    }
}

